<!--
 * @Author: curse-the-west m18811101669@163.com
 * @Date: 2022-09-22 15:03:37
 * @LastEditors: nainaiyi s15037075861@163.com
 * @LastEditTime: 2022-10-25 10:55:35
 * @FilePath: /a2001-a_project/websokcot/index.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #box {
            width: 100px;
            height: 100px;
            position: relative;
            background: red;
        }
    </style>
</head>

<body>
    <input type="text" name="" id="sends">
    <div id="content">

    </div>
</body>
<script>
    let box = document.getElementById('box');
    let sends = document.getElementById('sends');
    let content = document.getElementById('content');
    const ws = new WebSocket('ws://localhost:9000');
    let html = '';

    // 解析Blob进制流





    ws.onopen = function () {
        // 连接的时候触发
        if (ws.readyState === 1) {
            console.log('连接成功')
        }
    }

    ws.onmessage = function (msg) {

        html += `<div>她说：${JSON.parse(msg.data)}</div>`;
        content.innerHTML = html;
    }

    sends.onchange = function (e) {

        html += `<div>我说：${e.target.value}</div>`;

        content.innerHTML = html;
        ws.send(e.target.value);
    }

</script>

</html>